package com.coder520.POI.common.poiUtils.excel;

import static org.hamcrest.CoreMatchers.nullValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;

import com.coder520.POI.user.entity.User;

public class Excel2Data {
	private String filePath;
	private String sheetName;
	HSSFWorkbook wb;
	public Excel2Data(String filePath,String sheetName) {
		this.filePath=filePath;
		this.sheetName=sheetName;
	}
	@SuppressWarnings("null")
	public List<User> CreateUserList(){
		List<User> users =new ArrayList<User>();
		try {
			wb = new HSSFWorkbook(new FileInputStream(filePath));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("解析Excel文件出错");
			e.printStackTrace();
		}
		HSSFSheet sheet=wb.getSheet(sheetName);
		int rowLength=sheet.getLastRowNum();
		for (int i = 2; i <= rowLength; i++) {
			User user = new User();
			HSSFRow row=sheet.getRow(i);
			HSSFCell cell=row.getCell(0);
			user.setId((int) cell.getNumericCellValue());
			HSSFCell cell1=row.getCell(1);
			user.setUsername(cell1.getStringCellValue());
			HSSFCell cell2=row.getCell(2);
			user.setBirthday(cell2.getDateCellValue());
			HSSFCell cell3=row.getCell(3);
			user.setSex(cell3.getStringCellValue());
			HSSFCell cell4=row.getCell(4);
			user.setAddress(cell4.getStringCellValue());
			users.add(user);
		}
		return users;
		
		
	}
}
